from module.static_module.parent.view import SubWindowView
from tools.view import *


class RunningView(SubWindowView):
    def __init__(self, main_window_view):
        # 定制 Running 类的特殊布局
        super().__init__(main_window_view)
        self.frame = self.main_window_view.running_frame
        # 创建左上角文本
        self.class_name = Module.Running.value
        tk.Label(self.frame, text=self.class_name).pack()

        # 创建canvas
        self.canvas = tk.Canvas(self.frame, height=120)
        self.canvas.pack()

        # 创建滑轨文本框
        scrolled_text, self.text = create_scrolled_text(self.frame)
        scrolled_text.pack(expand=True, fill="both")

        # 根据当前module的值，修改布局
        self.change_layout()

    def change_layout(self):
        if self.main_window_view.master.file_manager.now_dynamic_module in conf.Framework.show_canvas_module.value:
            self.canvas.config(height=120)
        else:
            self.canvas.config(height=0)

    def del_canvas(self):
        # 清空canvas
        self.canvas.delete("all")

    def draw(self, draw_data_x: list, draw_data_y: list[list]):
        self.del_canvas()
        # 绘制线
        for i, x in enumerate(draw_data_x):
            if i == 0:
                continue
            x1 = draw_data_x[i - 1]
            x2 = draw_data_x[i]

            y1 = draw_data_y[i - 1][0]
            y2 = draw_data_y[i][0]
            self.canvas.create_line(x1, y1, x2, y2, width=2, fill="red")
            y1_f = draw_data_y[i - 1][1]
            y2_f = draw_data_y[i][1]
            self.canvas.create_line(x1, y1_f, x2, y2_f, width=2, fill="black")

        pass
